﻿namespace Access.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using Access;
    using BaseClass.Chung;
    using BaseClass.Duoc;
    using System;
    using System.Data;

    public class DNhapKho
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _conn;
        private General _xuli;

        public DNhapKho()
        {
            this._condata = new ConnectDatabase();
            this._xuli = new General(this._condata);
        }

        public DNhapKho(ConnectDatabase condata)
        {
            this._condata = condata;
            this._xuli = new General(this._condata);
        }

        public long f_get_capsophieu()
        {
            long num = 0L;
            try
            {
                num = long.Parse(this._xuli.f_Get_dulieu("select max(" + dtoDNhapKho.columnSOPHIEU + ") as sophieu  from " + this._xuli.pSchema + "." + dtoDNhapKho.tableName + " where to_char(" + dtoDNhapKho.columnNGAY + ",'yyyy')='" + DateTime.Now.ToString("yyyy") + "'").Tables[0].Rows[0][0].ToString());
            }
            catch
            {
            }
            return (num + 1L);
        }

        public bool f_huyphieunhap(long id)
        {
            int num = -1;
            try
            {
                NpgsqlConnection connect = new NpgsqlConnection(this._condata.StringConnect);
                connect.Open();
                NpgsqlTransaction transaction = connect.BeginTransaction();
                try
                {
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDNhapKhoCT.tableName, " where ", dtoDNhapKhoCT.columnIDNHAPKHO, "=", id }), connect);
                    num = -1;
                    num = this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDNhapKho.tableName, " where ", dtoDNhapKho.columnID, "=", id }), connect);
                    if (num > -1)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                catch
                {
                    transaction.Rollback();
                }
                finally
                {
                    connect.Close();
                    connect.Dispose();
                }
            }
            catch
            {
            }
            return (num > -1);
        }

        public long f_insert_nhapkho(dtoDNhapKho nhapkho, dtoDNhapKhoCT[] chitiet)
        {
            long num2;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            try
            {
                nhapkho.pID = long.Parse(this._xuli.f_Get_CapID());
                if (!nhapkho.Insert(this._conn))
                {
                    return 0L;
                }
                NpgsqlTransaction transaction = this._conn.BeginTransaction();
                int index = 0;
                index = 0;
                while (index < chitiet.Length)
                {
                    chitiet[index].pIDNHAPKHO = nhapkho.pID;
                    if (chitiet[index].pID != 0L)
                    {
                        if (!chitiet[index].Update(this._conn))
                        {
                            break;
                        }
                    }
                    else
                    {
                        chitiet[index].pID = long.Parse(this._xuli.f_Get_CapID());
                        if (!chitiet[index].Insert(this._conn))
                        {
                            chitiet[index].pID = 0L;
                            break;
                        }
                    }
                    index++;
                }
                if (index == chitiet.Length)
                {
                    transaction.Commit();
                    return nhapkho.pID;
                }
                transaction.Rollback();
                nhapkho.Delete();
                num2 = 0L;
            }
            catch
            {
                num2 = 0L;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return num2;
        }

        public DataTable f_loaddata_nhapkho(long id, string tungay, string denngay, HangSo.LoaiNhapKho.enLoaiNhapKho loainhap)
        {
            DataTable table = new DataTable();
            string str = this._condata.Schema + ".";
            string sql = string.Concat(new object[] { 
                "select a.", dtoDNhapKho.columnID, " as id,a.", dtoDNhapKho.columnSOPHIEU, " as sophieu,a.", dtoDNhapKho.columnHOADON, " as hoadon,to_char(", dtoDNhapKho.columnNGAY, ",'dd/mm/yyyy hh24:mi') as ngay,a.", dtoDNhapKho.columnIDKHO, " as idkho,a.", dtoDNhapKho.columnIDDANGNHAP, " as iddangnhap,a.", dtoDNhapKho.columnIDNHACUNGCAP, " as idnhacc,b2.", dtoDDMNhaCungCap.columnTEN, 
                " as tennhacc,b.", dtoDMDangNhap.columnTENNGUOIDUNG, " as tennguoidangnhap,a.", dtoDNhapKho.columnLOAINHAP, " as loainhap,a.", dtoDNhapKho.columnNGUOIGIAO, " as nguoigiao,to_char(", dtoDNhapKho.columnNGAYHD, ",'dd/mm/yyyy') as ngayhd,b2.", dtoDDMNhaCungCap.columnDIACHI, " from ", str, dtoDNhapKho.tableName, " a left join ", str, dtoDMDangNhap.tableName, 
                " b on a.", dtoDNhapKho.columnIDDANGNHAP, "=b.", dtoDMDangNhap.columnID, " left join ", str, dtoDDMNhaCungCap.tableName, " b2 on a.", dtoDNhapKho.columnIDNHACUNGCAP, "=b2.", dtoDDMNhaCungCap.columnID, " where 1=1 ", (id == 0L) ? "" : string.Concat(new object[] { " and a.", dtoDNhapKho.columnID, "=", id }), (tungay == "") ? "" : (" and to_timestamp(to_char(a." + dtoDNhapKho.columnNGAY + ",'dd/mm/yyyy'),'dd/mm/yyyy')>=to_timestamp('" + tungay + "','dd/mm/yyyy')"), (denngay == "") ? "" : (" and to_timestamp(to_char(a." + dtoDNhapKho.columnNGAY + ",'dd/mm/yyyy'),'dd/mm/yyyy')<=to_timestamp('" + denngay + "','dd/mm/yyyy')"), " and a.", 
                dtoDNhapKho.columnLOAINHAP, "=", loainhap.GetHashCode(), " order by a.", dtoDNhapKho.columnNGAY, " desc"
             });
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public DataTable f_loaddata_nhapkhoct(long idnhapkho)
        {
            DataTable table = new DataTable();
            string str = this._condata.Schema + ".";
            string sql = string.Concat(new object[] { 
                "select b.", dtoDNhapKhoCT.columnIDNHAPKHO, " as idnhapkho,b.", dtoDNhapKhoCT.columnID, " as id,b2.", dtoDDMDuocPham.columnID, " as idvp,b2.", dtoDDMDuocPham.columnTEN, " as tenvp,b2.", dtoDDMDuocPham.columnMA, " as mavp,b2.", dtoDDMDuocPham.columnDVT, " as dvt,b2.", dtoDDMDuocPham.columnMAVACH, " as mavach,b.", dtoDNhapKhoCT.columnLO, 
                " as losx,b.", dtoDNhapKhoCT.columnSOLUONG1, " as soluong1,b.", dtoDNhapKhoCT.columnSOLUONG2, " as soluong2,b.", dtoDNhapKhoCT.columnSOLUONG, " as soluong,b.", dtoDNhapKhoCT.columnGIAMUA, " as giamua,b.", dtoDNhapKhoCT.columnGIABAN, " as giaban,b.", dtoDNhapKhoCT.columnCHIETKHAU, ",b.", dtoDNhapKhoCT.columnGIANHAP, ",b.", dtoDNhapKhoCT.columnSOLUONGXUAT, 
                " as soluongxuat,b.", dtoDNhapKhoCT.columnSOLUONG, "*b.", dtoDNhapKhoCT.columnGIAMUA, " as thanhtien,b.", dtoDNhapKhoCT.columnSOLUONG1, "*b.", dtoDNhapKhoCT.columnGIAMUA, " as thanhtienmua,b.", dtoDNhapKhoCT.columnSOLUONG1, "*b.", dtoDNhapKhoCT.columnGIABAN, " as thanhtiennhap,b.", dtoDNhapKhoCT.columnIDDANGNHAP, " as iddangnhap,b.", dtoDNhapKhoCT.columnHANGSX, 
                " as hangsx,b.", dtoDNhapKhoCT.columnTHUE, " as thue,to_char(b.", dtoDNhapKhoCT.columnHANDUNG, ",'dd/mm/yyyy') as handung,b.", dtoDNhapKhoCT.columnIDVITRIKHO, " as idvitrikho,case when b3.", dtoDCapNhatGiaBan.columnGIABANLE_MOI, " is null then 0 else b3.", dtoDCapNhatGiaBan.columnGIABANLE_MOI, " end as giabanle,case when b3.", dtoDCapNhatGiaBan.columnGIABANSI_MOI, " is null then 0 else b3.", dtoDCapNhatGiaBan.columnGIABANSI_MOI, " end as giabansi,0.0 as idphieudathangct from ", str, 
                dtoDNhapKho.tableName, " a inner join ", str, dtoDNhapKhoCT.tableName, " b on a.", dtoDNhapKho.columnID, "=b.", dtoDNhapKhoCT.columnIDNHAPKHO, " inner join ", str, dtoDDMDuocPham.tableName, " b2 on b.", dtoDNhapKhoCT.columnIDDUOCPHAM, "=b2.", dtoDDMDuocPham.columnID, " left join ", 
                str, dtoDCapNhatGiaBan.tableName, " b3 on b.", dtoDNhapKhoCT.columnIDDUOCPHAM, "=b3.", dtoDCapNhatGiaBan.columnIDSANPHAM, " where a.", dtoDNhapKho.columnID, "=", idnhapkho, " order by a.", dtoDNhapKho.columnNGAY, " desc"
             });
            return this._xuli.f_Get_dulieu(sql).Tables[0];
        }

        public long f_loaddata_nhapkhoct_gannhat(long idduocpham)
        {
            DataTable table = new DataTable();
            string str = this._condata.Schema + ".";
            string sql = string.Concat(new object[] { 
                "select b.", dtoDNhapKhoCT.columnIDDUOCPHAM, " as id from ", str, dtoDNhapKho.tableName, " a inner join ", str, dtoDNhapKhoCT.tableName, " b on a.", dtoDNhapKho.columnID, "=b.", dtoDNhapKhoCT.columnIDNHAPKHO, " where b.", dtoDNhapKhoCT.columnIDDUOCPHAM, "=", idduocpham, 
                " order by a.", dtoDNhapKho.columnNGAY, " desc"
             });
            table = this._xuli.f_Get_dulieu(sql).Tables[0];
            return 0L;
        }

        public decimal f_update_nhapkho(dtoDNhapKho ToaThuoc, dtoDNhapKhoCT[] chitiet)
        {
            decimal num2;
            this._conn = new NpgsqlConnection(this._condata.StringConnect);
            this._conn.Open();
            NpgsqlTransaction transaction = this._conn.BeginTransaction();
            try
            {
                if (!ToaThuoc.Update(this._conn))
                {
                    transaction.Rollback();
                    return 0M;
                }
                string str = "";
                int index = 0;
                index = 0;
                while (index < chitiet.Length)
                {
                    if (chitiet[index].pID != 0L)
                    {
                        if (!chitiet[index].Update(this._conn))
                        {
                            break;
                        }
                    }
                    else
                    {
                        chitiet[index].pID = long.Parse(this._xuli.f_Get_CapID());
                        if (!chitiet[index].Insert(this._conn))
                        {
                            break;
                        }
                    }
                    str = str + chitiet[index].pID + ",";
                    index++;
                }
                if (index == chitiet.Length)
                {
                    transaction.Commit();
                    this._xuli.f_Update_dulieu(string.Concat(new object[] { "delete from ", this._xuli.pSchema, ".", dtoDNhapKhoCT.tableName, " where ", dtoDNhapKhoCT.columnID, " not in(", str.Trim(new char[] { ',' }), ")  and ", dtoDNhapKhoCT.columnSOLUONGXUAT, "=0 and ", dtoDNhapKhoCT.columnIDNHAPKHO, "=", ToaThuoc.pID }));
                    return ToaThuoc.pID;
                }
                transaction.Rollback();
                num2 = 0M;
            }
            catch
            {
                transaction.Rollback();
                num2 = 0M;
            }
            finally
            {
                this._conn.Close();
                this._conn = null;
            }
            return num2;
        }
    }
}

